import { merge } from 'lodash';

const env = process.env.NODE_ENV || "dev";
const prefix = process.env.PUBLISH_PREFIX || 'dev'; // deploy || test, 适应不同的发布ip, 使邀请链接能与之对应
const protocol = process.env.PUBLISH_PROTOCOL || 'http';

const isProduction = () => env.indexOf('prod') >= 0 || env.indexOf('deployTest') >= 0;

let skipToken = true;
let skipUpload = true;
let skipWechatMp = true;

interface skipFunc { (): boolean }
interface setSkipFunc { (v: boolean) }

interface Config {
    protocol: string;
    app: {
        port: number;
    };
    socket: {
        port: number;
    };
    db: {
        url?: string;
        username?: string;
        password?: string;
        database?: string;
        options?: {
            host?: string;
            dialect?: string;
            storage?: string;
            port?: number;
            logging?: boolean;
        }
    };
    redis: {
        url: string;
    };
    captcha: {
        factoryName?: string;
        ttl: number;
    };
    token: {
        factoryName: string,
        securityCode: string;
        ttl: number | string;
    };
    email: {
        trigger: {
            apiKey: string;
            apiUser: string;
        };
        batch: {
            apiKey: string;
            apiUser: string;
        }
    };
    sms: {
        smsKey: string;
        smsUser: string;
    };
    wechat: {
        appId: string;
        appSecret: string;
    };
    wechatmp: {
        appId: string;
        appSecret: string;
        token: string;
        ttl: number;
        userId: string;
        templateId: any;
    };
    skip?: {
        token?: skipFunc;
        setToken?: setSkipFunc;
        upload?: skipFunc;
        setUpload?: setSkipFunc;
        captcha?: boolean;
        socket?: boolean;
        wechatMp?: skipFunc;
        setWechatMp?: setSkipFunc;
        noticeExcludeSelf?: boolean;
    };
    qiniu: {
        bucket: any;
        dnttl: string;
        domain: any;
        accessKey: string;
        secretKey: string;
    };
    constant: any;
    log: {
        level: string;
    };
    enableCooperateAuth: boolean;
    enableLogOutputInConsole: boolean;
}

const qiniu_dev = {
    bucket: 'car-dev',
    domain: 'http://onrsruqf7.bkt.clouddn.com',
    accessKey: 'xSdDwSS238cxBCQ5a8alXDL9OAty_bwwVhQ9mAgp',
    secretKey: 'qt8YNEU3dCauH6_7At4_5Al9__XpVtHszY6N5m41',
    dnttl: '7 days'
};


// 公司七牛帐号
const qiniu_co = {
    bucket: 'car-dev',
    domain: 'http://onrsruqf7.bkt.clouddn.com',
    accessKey: 'xSdDwSS238cxBCQ5a8alXDL9OAty_bwwVhQ9mAgp',
    secretKey: 'qt8YNEU3dCauH6_7At4_5Al9__XpVtHszY6N5m41',
    dnttl: '7 days'
};

// 微信公众号,测试用
const wechatmp_dev = {
    appId: 'wx3aebdae9ae559cf4',
    appSecret: 'd4624c36b6795d1d99dcf0547af5443d',
    token: 'l8kG22kS7',
    ttl: 60 * 60 * 24 * 7,
    userId: 'gh_9e3a3ab20df7',
    templateId: {
        login: 'YgcyJrkhBxfLoWDLc8JV8VICiR3jGikCv7nTuJY6aOg',
        bind: 'ZEJ307yYbEG_8kfWKOnhoA_Q685QnbnRtuJ4u3E2oAw',
        newTask: 'Ff7zHVCGE86pYtED2Ubbmn88lO5vRNtBE4cjkBYhVu8',
        taskChange: 'BwJETvGAY3xVuiAYIprWoITtBSO9-NS12xyHcql_G9g',
    }
};

// 微信公众号,正式
const wechatmp_co = {
    appId: 'wx3aebdae9ae559cf4',
    appSecret: 'd4624c36b6795d1d99dcf0547af5443d',
    token: 'l8kG22kS7',
    ttl: 60 * 5,
    userId: 'gh_9e3a3ab20df7',
    templateId: {
        login: 'YgcyJrkhBxfLoWDLc8JV8VICiR3jGikCv7nTuJY6aOg',
        bind: 'ZEJ307yYbEG_8kfWKOnhoA_Q685QnbnRtuJ4u3E2oAw',
        newTask: 'Ff7zHVCGE86pYtED2Ubbmn88lO5vRNtBE4cjkBYhVu8',
        taskChange: 'BwJETvGAY3xVuiAYIprWoITtBSO9-NS12xyHcql_G9g',
    }
};

const base: Config = {
    protocol: protocol,
    app: { port: 4010 },
    socket: {
        port: 4010
    },
    db: { username: 'car', password: 'carServer_01', database: 'car_test', options: { host: 'rm-bp18346uf7smf7f39o.mysql.rds.aliyuncs.com', dialect: 'mysql', logging: false } },
    redis: { url: 'redis://localhost:6379/0' },
    captcha: {
        factoryName: "random",
        ttl: 60 * 60 * 5,
    },
    token: {
        factoryName: 'prod',
        securityCode: '5dffa227-b950-4559-9f20-3f1831baaa92',
        ttl: '30 days'
    },
    email: {
        trigger: {
            apiKey: '0DwXwVnHm33Rljjx',
            apiUser: 'chipwing_test_1Gg7dK',
        },
        batch: {
            apiKey: '0DwXwVnHm33Rljjx',
            apiUser: 'chipwing_test_batch',
        },
    },
    sms: {
        smsKey: 'HUQ50M5OCehllYrfsEYlZ0IHZRUehMWO',
        smsUser: 'jiebaoyun_test'
    },
    wechat: {
        appId: "wxaf4fc10cba78db30",
        appSecret: "d4624c36b6795d1d99dcf0547af5443d",
    },
    wechatmp: wechatmp_dev,
    skip: {
        wechatMp: () => prefix === 'www' ? false : skipWechatMp,
        setWechatMp: (v) => { skipWechatMp = v },
        upload: () => isProduction() ? false : skipUpload,
        setUpload: (v) => { skipUpload = v }
    },
    qiniu: isProduction() ? qiniu_co : qiniu_dev,
    constant: {
        host: `${protocol}://${prefix}.chipcoo.com`, // 邀请时发送的链接
        invitedPage: '/invited-join.html' // 前台资源页面
    },
    log: {
        level: 'DEBUG',
    },
    enableCooperateAuth: true,
    enableLogOutputInConsole: true
};

const specific = {
    autoTest: {
        app: { port: null },
        socket: { port: null },
        db: { username: 'lemon', password: 'Sophia_01', database: 'lemon', options: { host: 'rm-bp18346uf7smf7f39o.mysql.rds.aliyuncs.com', dialect: 'mysql', logging: false } },
        redis: { url: 'redis://localhost:6379/0' },
        captcha: {
            factoryName: "fixed",
            ttl: 20
        },
        skip: {
            token: () => skipToken,
            setToken: (v) => { skipToken = v },
            geetest: true,
            captcha: true,
        },
        constant: {
            includeTestEvent: true
        },
        enableCooperateAuth: false,
    },
    dev: {
        db: { username: 'lemon', password: 'Sophia_01', database: 'lemon', options: { host: 'rm-bp18346uf7smf7f39o.mysql.rds.aliyuncs.com', dialect: 'mysql', logging: false } },
        redis: { url: 'redis://localhost:6379/0' },
        token: {
            factoryName: 'dev',
        },
        skip: {
            captcha: true,
            noticeExcludeSelf: true
        },
        constant: {
            host: 'http://car.com:8080'
        },
        enableCooperateAuth: false,
    },
    local: {
        db: { username: 'lemon', password: 'Sophia_01', database: 'lemon', options: { host: 'rm-bp18346uf7smf7f39o.mysql.rds.aliyuncs.com', dialect: 'mysql', logging: false } },
        token: {
            factoryName: 'dev',
        },
        skip: {
            captcha: true,
            noticeExcludeSelf: true
        },
        constant: {
            host: 'http://car.com:8080'
        },
        enableCooperateAuth: false,
    },
    prod: {
        db: { username: 'lemon', password: 'Sophia_01', database: 'lemon', options: { host: 'rm-bp18346uf7smf7f39o.mysql.rds.aliyuncs.com', dialect: 'mysql', logging: false } },
        redis: {
            url: 'redis://10.9.145.228:6379/0'
        },
        log: {
            level: 'INFO',
        },
    },
};
console.log(`Env:${env}`);
export default merge(base, specific[env]) as Config;